var SiteTracker = function(s, p, r, u) {
    if (s != undefined && s != null) {
        this.site = s;
    }

    if (p != undefined && p != null) {
        this.page = p;
    }

    if (r != undefined && r != null) {
        this.referer = r;
    }

    if (u != undefined && u != null) {
        this.uid = u;
    }
};

SiteTracker.prototype.getCookie = function(c_name) {
    if (document.cookie.length == 0) return "";

    c_start = document.cookie.indexOf(c_name + "=");
    if (c_start == -1) return "";

    c_start = c_start + c_name.length+1;
    c_end = document.cookie.indexOf(";", c_start);
    if (c_end == -1) c_end = document.cookie.length;
    return unescape(document.cookie.substring(c_start, c_end));
};

SiteTracker.prototype.track = function() {
    this.buildParams();

    var src = "http://s.anjuke.com/st?";
    for (var k in this.params) {
        src += k + "=" + encodeURIComponent(this.params[k]) + "&";
    }
    src = src.substr(0, src.length-1);

    var script = document.createElement("script");
    script.src = src;
    script.async = true;
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);
};

SiteTracker.prototype.buildParams = function() {
    var href  = document.location.href;

    var guid = this.getCookie(this.nGuid   || "aQQ_ajkguid");
    var ctid = this.getCookie(this.nCtid   || "ctid");
    var luid = this.getCookie(this.nLiu    || "lui");
    var ssid = this.getCookie(this.nSessid || "sessid");
    var uid  = this.getCookie(this.nUid    || "ajk_member_id");

    if (this.uid != undefined && this.uid != null) {
        uid = this.uid;
    }

    var method = "";
    if (this.method != undefined && this.method != null) {
        method = this.method;
    }

    this.params = new Object();
    this.params.p = this.page;
    this.params.h = href;
    this.params.r = this.referer;
    this.params.site = this.site;
    this.params.guid = guid;
    this.params.ssid = ssid;
    this.params.uid  = uid;
    this.params.t = new Date().getTime();
    this.params.ctid = ctid;
    this.params.luid = luid;
    this.params.m = method;

    if (this.cst != undefined && /[0-9]{13}/.test(this.cst)) {
        this.params.lt = this.params.t - parseInt(this.cst);
    }

    if (this.pageName != undefined) {
        this.params.pn = this.pageName;
    }

    if (this.customParam != undefined) {
        this.params.cp = this.customParam;
    }
};

SiteTracker.prototype.setSite = function(s) {
    this.site = s;
};

SiteTracker.prototype.setPage = function(p) {
    this.page = p;
};

SiteTracker.prototype.setPageName = function(n) {
    this.pageName = n;
};

SiteTracker.prototype.setCookieNames = function(c) {
    this.cookNames = c;
};

SiteTracker.prototype.setReferer = function(r) {
    this.referer = r;
};

SiteTracker.prototype.setUid = function(u) {
    this.uid = u;
};

SiteTracker.prototype.setMethod = function(m) {
    this.method = m;
};

SiteTracker.prototype.setNGuid = function(n) {
    this.nGuid = n;
};

SiteTracker.prototype.setNCtid = function(n) {
    this.nCtid = n;
};

SiteTracker.prototype.setNLiu = function(n) {
    this.nLiu = n;
};

SiteTracker.prototype.setNSessid = function(n) {
    this.nSessid = n;
};

SiteTracker.prototype.setNUid = function(n) {
    this.nUid = n;
};

SiteTracker.prototype.setCst = function(n) {
    this.cst = n;
};

SiteTracker.prototype.setCustomParam = function(v) {
    this.customParam = v;
}
